/*
 * table_Option.c
 *
 *  Created on: 2012/03/30
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"

typedef struct _OPTIONS {
	U2  optId;
	S4  optIntParam;
	AST astStrParam;
}OPTIONS, *POPTIONS;


#define BufType  PSZ
#define Buffer stOptBufferManager.memoryBlocks
#define Buflen stOptBufferManager.maxRecordNum
#define  bufLenPerBlk  (stOptBufferManager.astMaxNum_Block)

#define TableSize  (sizeof(OPTIONS))
#define TableType  POPTIONS
#define stTable stOptTblManager.memoryBlocks
#define maxTable stOptTblManager.maxRecordNum
#define  maxNumPerBlk  (stOptTblManager.astMaxNum_Block)

static TABLEMANAGER stOptBufferManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__  "_buffer",
//		U2					con_usSize;		constant
		1,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};

static VOID resetBufListener( U4  procid, U2 index )
{
	if( procid == stOptBufferManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			Buffer[ index-1] = NULL;
		}
	}
}
static BufType core_getBuf( AST index )
{
	BufType temp = (BufType)Check_Com( &stOptBufferManager, resetBufListener, index );
	if( temp != NULL ){
		return &temp[index%bufLenPerBlk];
	}
	return NULL;
}
static BOOL AllocBuffer( AST astReqArg )
{
	return AllocBuffer_Com( &stOptBufferManager, astReqArg, resetBufListener );
}
//		BOOL (*clear)( VOID );
static BOOL ClearBuffer( VOID )
{
	return Clear_Com( &stOptBufferManager );
}
static BOOL ResetBuffer( VOID )
{
	return Reset_Com( &stOptBufferManager );
}
static TABLEMANAGER stOptTblManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stOptTblManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static AST getMaxLineNum( VOID )
{
	return maxTable;
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stOptTblManager, resetListener, index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	BOOL bRet1 = ClearBuffer();
	BOOL bRet2 = Clear_Com( &stOptTblManager );
	return bRet1 && bRet2 ;
}
static BOOL ResetTable( VOID )
{
	BOOL bRet1 = ResetBuffer();
	BOOL bRet2 = Reset_Com( &stOptTblManager );
	return bRet1 && bRet2 ;
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stOptTblManager, astReqArg, resetListener );
}
/*  get method */
static CHAR * getOptStr( AST index )
{
	BufType pTbl = core_getBuf( index );
	if( pTbl == NULL )
	{
		return "";
	}
	return pTbl;
}
/*  set method */
static AST setOptStr( PSZ str )
{
	S4 lenBuf = str_length(str);
	AST astHeaderNo;
	if( lenBuf > 0 && lenBuf < MemGetBlockSize( ))
	{
		if( AllocBuffer( lenBuf + 1 ) ) {
			lenBuf = Buflen - lenBuf;
			astHeaderNo = lenBuf/stOptBufferManager.astMaxNum_Block;
			strcpy( (PSZ)Buffer[astHeaderNo]+lenBuf%stOptBufferManager.astMaxNum_Block, str );
			*((PSZ)Buffer[astHeaderNo]+Buflen%stOptBufferManager.astMaxNum_Block) = '\0';
		}
		else
		{
			return 0;
		}
	}
	else
	{
		if ( 0 == AllocBuffer( lenBuf + 1 ) )
		{
			return 0;
		}
		lenBuf = Buflen;
	}
	return lenBuf;
}
//ACCESS_U2_FIELD  optId;
static U2 getOptId( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->optId;
}
static U2 setOptId( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->optId = value;
}
//ACCESS_S4_FIELD  intValue;
static S4 getIntValue( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->optIntParam;
}
static S4 setIntValue( AST index, S4 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->optIntParam = value;
}
//ACCESS_STR_FIELD  strValue;
static CHAR* getStrValue( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return "";
	}
	return getOptStr(pTbl->astStrParam);
}
static AST setStrValue( AST index, PSZ str )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astStrParam = setOptStr(str);
}
static BOOL hasStrValue( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astStrParam != 0 ;
}
const COREOPT_ACCESS stCoreOptAccess = {
//		BOOL (*clear)( VOID );
//		BOOL (*reset)( VOID );
//		BOOL (*alloc)( AST );
//		AST  (*getMax)( VOID );
		ClearTable,
		ResetTable,
		AllocTable,
		getMaxLineNum,

//ACCESS_U2_FIELD  optId;
		{
				getOptId,
				setOptId
		},
//ACCESS_IDX_FIELD  intValue;
		{
				getIntValue,
				setIntValue
		},
//ACCESS_STR_FIELD  strValue;
		{
				getStrValue,
				setStrValue
		},
		hasStrValue
};
